/*
 * 文件名：StsMonthCollectServiceImpl.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月20日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.sts.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.job.bpa.service.IncaeSureService;
import top.infopub.job.bpa.service.OutcaeSureService;
import top.infopub.job.fia.service.FeeNoteService;
import top.infopub.job.fia.service.IncomeBillService;
import top.infopub.job.fia.service.PayBillDetailService;
import top.infopub.job.fia.service.PayBillService;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.mkt.service.DiscloseProService;
import top.infopub.job.sts.service.MonthCollectService;
import top.infopub.job.sts.service.StsMonthCollectService;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.mkt.DisclosePro;
import top.infopub.model.sts.MonthCollect;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;


@Service
public class StsMonthCollectServiceImpl implements StsMonthCollectService {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private OutcaeSureService outcaeSureService;

    @Autowired
    private IncaeSureService incaeSureService;

    @Autowired
    private DiscloseProService discloseProService;

    @Autowired
    private IncomeBillService incomeBillService;

    @Autowired
    private PayBillService payBillService;

    @Autowired
    private PayBillDetailService payBillDetailService;

    @Autowired
    private MonthCollectService monthCollectService;

    @Autowired
    private FeeNoteService feeNoteService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsMonthCollectJob() {
        //查询所有交底的项目
        Map<String, Object> discloseProWhere = Maps.newHashMap();
        discloseProWhere.put("processStatus", "9");
        List<DisclosePro> disclosePros = discloseProService.selectByMapWhere(discloseProWhere);
        for (DisclosePro disclosePro : disclosePros) {
            MonthCollect monthCollect = new MonthCollect();
            //往表里存放项目信息
            monthCollect.setProCode(disclosePro.getProCode());
//            monthCollect.setProName(disclosePro.getProName());
            //往表里存放公司信息
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setProjectCode(disclosePro.getProCode());
            orgInfo.setState(1);
            List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
            if (orgInfos.size() > 0) {
                // 注意：不归集南充项目
                if (StringUtils.endsWithIgnoreCase(orgInfos.get(0).getId(),
                    BizParaUtil.get("land_org_id"))) {
                    continue;
                }
                monthCollect.setOrgProName(orgInfos.get(0).getCompName());
                monthCollect.setHrOrgProId(orgInfos.get(0).getId());
                OrgInfo parentOrgInfo = new OrgInfo();
                parentOrgInfo.setId(orgInfos.get(0).getParentId());
                parentOrgInfo.setState(1);
                List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
                if (parentOInfos.size() > 0) {
                    monthCollect.setHrOrgAreaName(parentOInfos.get(0).getCompName());
                    monthCollect.setHrOrgAreaId(parentOInfos.get(0).getId());
                }
            }
            //存放时间信息
            monthCollect.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            monthCollect.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            monthCollect.setStsMonth(ParserUtil.prevMonth());//上个月
            //产值下浮后(对内)
            Map<String, Object> innerSureWhere = Maps.newHashMap();
            innerSureWhere.put("sureMonth", monthCollect.getStsMonth());
            innerSureWhere.put("proCode", monthCollect.getProCode());
            BigDecimal proQaOfInnerSure = incaeSureService.selectProQaOfInnerSure(innerSureWhere);
            monthCollect.setInAmountAfter(proQaOfInnerSure);
            //产值下浮后(对外)
            Map<String, Object> outSureWhere = Maps.newHashMap();
            outSureWhere.put("sureMonth", monthCollect.getStsMonth());
            outSureWhere.put("proCode", monthCollect.getProCode());
            BigDecimal proQaOfOutSure = outcaeSureService.selectProQaOfOutSure(outSureWhere);
            monthCollect.setOutAmountAfter(proQaOfOutSure);
            //收款(融资收款)
            Map<String, Object> proQaWhere = Maps.newHashMap();
            proQaWhere.put("incomeDateStart", ParserUtil.prevMonth());
            proQaWhere.put("incomeDateEnd", ParserUtil.currentMonth());
            proQaWhere.put("hrOrgProId", monthCollect.getHrOrgProId());
            BigDecimal incomeOfCashIn = incomeBillService.selectIncomeOfCashIn(proQaWhere);
            monthCollect.setFinIncome(incomeOfCashIn);
            //收款(政府来源收款)
            Map<String, Object> incomeWhere = Maps.newHashMap();
            incomeWhere.put("incomeDateStart", ParserUtil.prevMonth());
            incomeWhere.put("incomeDateEnd", ParserUtil.currentMonth());
            incomeWhere.put("hrOrgProId", monthCollect.getHrOrgProId());
            BigDecimal incomeAmountOfCashIn = incomeBillService.selectIncomeAmountOfCashIn(incomeWhere);
            monthCollect.setGdIncome(incomeAmountOfCashIn);
            //支出(建安费)
            Map<String, Object> payBillActPayAmountOne = Maps.newHashMap();
            payBillActPayAmountOne.put("startPayDate", ParserUtil.prevMonth());
            payBillActPayAmountOne.put("endPayDate", ParserUtil.currentMonth());
            payBillActPayAmountOne.put("proCode", monthCollect.getProCode());
            BigDecimal payBillActPayAmountOnes = payBillDetailService.selectPayBillActPayAmountOne(payBillActPayAmountOne);
            monthCollect.setCaeAmountCost(payBillActPayAmountOnes);
            //支出(建设工程其他费)
            Map<String, Object> payBillActPayAmountFour = Maps.newHashMap();
            payBillActPayAmountFour.put("startPayDate", ParserUtil.prevMonth());
            payBillActPayAmountFour.put("endPayDate", ParserUtil.currentMonth());
            payBillActPayAmountFour.put("proCode", monthCollect.getProCode());
            BigDecimal payBillActPayAmountfour = payBillDetailService.selectPayBillActPayAmountfour(payBillActPayAmountFour);
            monthCollect.setBuildOtherCost(payBillActPayAmountfour);
            //支出(代付总包费)
            Map<String, Object> payBillActPayAmountFive = Maps.newHashMap();
            payBillActPayAmountFive.put("startPayDate", ParserUtil.prevMonth());
            payBillActPayAmountFive.put("endPayDate", ParserUtil.currentMonth());
            payBillActPayAmountFive.put("proCode", monthCollect.getProCode());
            BigDecimal payBillActPayAmountfive = payBillDetailService.selectPayBillActPayAmountfive(payBillActPayAmountFive);
            monthCollect.setReplacePackCost(payBillActPayAmountfive);
            //支出(专业设备采购款)
            Map<String, Object> payBillActPayAmountSix1 = Maps.newHashMap();
            payBillActPayAmountSix1.put("startPayDate", ParserUtil.prevMonth());
            payBillActPayAmountSix1.put("endPayDate", ParserUtil.currentMonth());
            payBillActPayAmountSix1.put("proCode", monthCollect.getProCode());
            payBillActPayAmountSix1.put("payOutType", "24");
            BigDecimal payBillActPayAmountsix1 = payBillDetailService.selectPayBillActPayAmountSix(payBillActPayAmountSix1);
            monthCollect.setDeviceBuyCost(payBillActPayAmountsix1);
            //支出(项目运维费用)
            Map<String, Object> payBillActPayAmountSix2 = Maps.newHashMap();
            payBillActPayAmountSix2.put("startPayDate", ParserUtil.prevMonth());
            payBillActPayAmountSix2.put("endPayDate", ParserUtil.currentMonth());
            payBillActPayAmountSix2.put("proCode", monthCollect.getProCode());
            payBillActPayAmountSix2.put("payOutType", "32");
            BigDecimal payBillActPayAmountsix2 = payBillDetailService.selectPayBillActPayAmountSix(payBillActPayAmountSix2);
            monthCollect.setProOmCost(payBillActPayAmountsix2);
            //支出(保证金)
            Map<String, Object> payBillActPayAmountServen = Maps.newHashMap();
            payBillActPayAmountServen.put("startPayDate", ParserUtil.prevMonth());
            payBillActPayAmountServen.put("endPayDate", ParserUtil.currentMonth());
            payBillActPayAmountServen.put("proCode", monthCollect.getProCode());
            BigDecimal payBillActPayAmountserven = payBillDetailService.selectPayBillActPayAmountServen(payBillActPayAmountServen);
            monthCollect.setBondCost(payBillActPayAmountserven);
            //支出(管理费用)
            Map<String, Object> manageFeeWhere = Maps.newHashMap();
            manageFeeWhere.put("stsMonth", monthCollect.getStsMonth());
            manageFeeWhere.put("hrOrgProId", monthCollect.getHrOrgProId());
            BigDecimal manageCost = feeNoteService.selectProQaOfManageFee(manageFeeWhere);
            monthCollect.setManageCost(manageCost);
            //支出(财务费用)
            Map<String, Object> feeWhere = Maps.newHashMap();
            feeWhere.put("stsMonth", monthCollect.getStsMonth());
            feeWhere.put("hrOrgProId", monthCollect.getHrOrgProId());
            BigDecimal fiaCost = feeNoteService.selectProQaOfManageFeeOne(feeWhere);
            monthCollect.setFiaCost(fiaCost);
            //支出(税金)
            Map<String, Object> payBillActPayAmountSix3 = Maps.newHashMap();
            payBillActPayAmountSix3.put("startPayDate", ParserUtil.prevMonth());
            payBillActPayAmountSix3.put("endPayDate", ParserUtil.currentMonth());
            payBillActPayAmountSix3.put("proCode", monthCollect.getProCode());
            payBillActPayAmountSix3.put("payOutType", "20");
            BigDecimal taxCost = payBillDetailService.selectPayBillActPayAmountSix(payBillActPayAmountSix3);
            monthCollect.setTaxCost(taxCost);
            //支出(归还政府金额)
            Map<String, Object> payBillActPayAmountEight = Maps.newHashMap();
            payBillActPayAmountEight.put("startPayDate", ParserUtil.prevMonth());
            payBillActPayAmountEight.put("endPayDate", ParserUtil.currentMonth());
            payBillActPayAmountEight.put("proCode", monthCollect.getProCode());
            BigDecimal payBillActPayAmounteight = payBillDetailService.selectPayBillActPayAmountEight(payBillActPayAmountEight);
            monthCollect.setReturnGdCost(payBillActPayAmounteight);
            //数据状态(默认为有效)
            monthCollect.setDataFlag("1");
            //添加人 添加时间 最后修改人 最后修改时间
            monthCollect.setAddBy("system");
            monthCollect.setAddTime(new Date());
            monthCollect.setUptBy("system");
            monthCollect.setUptTime(new Date());
            monthCollectService.insert(monthCollect);
        }
    }
}